//
//  BuildFeedbackViewController.m
//  Estate
//
//  Created by houzhen on 2018/1/5.
//  Copyright © 2018年 ttsx. All rights reserved.
//

#import "BuildFeedbackViewController.h"
#import "TTAddFeebackViewController.h"
#import "RJTableCell.h"
#import "JLJLTableCell.h"
#import "MJRefresh.h"
#import "TTFeedBackModel.h"
#import <MJExtension.h>
@interface BuildFeedbackViewController()
@property(nonatomic,assign)int pageNum;
@property (nonatomic,copy) NSString * pageType;
@end

@implementation BuildFeedbackViewController
- (NSMutableArray *)dataArray{
    
    if(_dataArray==nil){
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView{
    
    if(_tableView==nil){
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT-64-49-50*BIZ)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [_tableView registerNib:[UINib nibWithNibName:@"RJTableCell" bundle:nil] forCellReuseIdentifier:@"RJCell"];
        [_tableView registerNib:[UINib nibWithNibName:@"JLJLTableCell" bundle:nil] forCellReuseIdentifier:@"JLJLCell"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    self.view.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    self.tableView.backgroundColor=[UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
 
    // 创建添加btn
    UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRECTS(260, 360, 100, 100)];
    [self.view addSubview:addBtn];
    [addBtn setImage:[UIImage imageNamed:@"add_2"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addContent) forControlEvents:UIControlEventTouchUpInside];
    
    self.pageNum=1;
    //上拉加载更多
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footRefresh)];
    self.tableView.mj_footer = footer;
    self.tableView.mj_footer.hidden = YES;
    
    //添加上拉 下滑刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headRefresh)];
    [self.tableView.mj_header beginRefreshing];
    

}
//下滑刷新
- (void)headRefresh{
    self.pageNum = 1;
    self.pageType = @"currentPage";
    [self addDataSource];

}

//上滑加载
-(void)footRefresh{
    self.pageNum++;
    self.pageType = @"sumPage";
    [self addDataSource];
}

-(void)addDataSource{

    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    // 获取项目ID
    NSString *proID = DEF_PERSISTENT_GET_OBJECT(@"proid");
    // 获取用户ID
    NSString *userID = DEF_PERSISTENT_GET_OBJECT(@"userid");
    [parameters setValue:@(self.pageNum) forKey:@"pageNum"];
    [parameters setValue:@(20) forKey:@"pageCount"];
    [parameters setValue:self.pageType forKey:@"pageType"];
    [parameters setValue:@"" forKey:@"startTime"];
    [parameters setValue:@"" forKey:@"endTime"];
    [parameters setValue:userID forKey:@"opinionsUserId"];
    [parameters setValue:proID forKey:@"proId"];
    [parameters setValue:@(NO) forKey:@"isLeaderAPP"];
   
    NSLog(@"%@",URL_OpinionsGetRecordList);
    [LSHttpTool Post:URL_OpinionsGetRecordList parameters:parameters success:^(id responseObject) {
        self.tableView.mj_footer.hidden = NO;

        NSLog(@"%@",responseObject);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        if([responseObject[@"code"] isEqualToString:@"1"]){
            NSMutableArray *dataArray = [TTFeedBackModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            TTFeedBackModel *model = [dataArray firstObject];
            self.dataArray = dataArray;
            if(self.dataArray.count>= model.total){
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView reloadData];
     
     } failure:^(NSError *error) {
         [self.tableView.mj_header endRefreshing];
         [self.tableView.mj_footer endRefreshing];
         [LSUtility showToastWithMessage:@"网络异常！" inView:self.view Andtime:1.2];
     }];
}
#pragma  mark-添加安全
- (void)addContent {
    TTAddFeebackViewController *detail=[[TTAddFeebackViewController alloc]init];
    WEAKSELF
    detail.block = ^{
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    detail.isEdit = YES;
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark - Table view data source



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 
    return self.dataArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    //得到改组下信息
    TTFeedBackModel *model=[self.dataArray objectAtIndex:indexPath.row];
    
    RJTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"RJCell" forIndexPath:indexPath];
    
    cell.title.text=model.opinionTitle;
    cell.name.text=model.opinionUserRealname;
    cell.time.text=model.createtimeStr;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70*BIZ;
}

#pragma mark-点击表格
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    long row=indexPath.row;
    //得到改组下信息
    TTFeedBackModel *model = [self.dataArray objectAtIndex:row];
    TTAddFeebackViewController *detail=[[TTAddFeebackViewController alloc]init];
    detail.isEdit = NO;
    detail.model = model;
    [self.navigationController pushViewController:detail animated:YES];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
